#include <pthread.h>
#include <iostream>

using std::cout;
using std::endl;

//定义了一把互斥锁
pthread_mutex_t mutex;
int gnum = 0;

int main()
{
    pthread_mutex_init(&mutex, NULL);
    //执行加锁操作
    //互斥锁没有被抢占时，加锁成功，之后就会继续往下执行
    pthread_mutex_lock(&mutex);
    cout << "main thread ...." << endl;
    //在一个线程中执行两次加锁操作，会阻塞该线程
    pthread_mutex_lock(&mutex);
    ++gnum;
    cout << "main thread: gnum = " << gnum << endl;
    
    //执行解锁操作
    pthread_mutex_unlock(&mutex);

    return 0;
}

